package com.gitee.wsl.common.chart.render.foreground.border

import android.graphics.*
import com.gitee.wsl.android.ui.base.BorderDrawable
import com.gitee.wsl.android.ui.ext.type.CanvasExt.getPaintFontHeight
import com.gitee.wsl.android.ui.ext.type.LineExt
import com.gitee.wsl.common.ui.draw.type.LineSpacingStyle
import com.gitee.wsl.common.ui.draw.type.RectType
import com.gitee.wsl.mathematics.geometry.shape.Rectangle


class BorderDrawableReader(innerRectangle: Rectangle) : BorderDrawable(innerRectangle) {


    var lineSpacingStyle = LineSpacingStyle.Solid
         set(value) {
             field=value
             when (value) {
                 LineSpacingStyle.Solid -> {}
                 LineSpacingStyle.Dot -> linePaint.pathEffect = LineExt.dotLineStyle
                 //虚实线
                 LineSpacingStyle.Dash -> linePaint.pathEffect = LineExt.dashLineStyle
                 else -> {}
             }
         }


    /**
     * 返回边框所占宽度
     * @return 边框所占宽度
     */
    val width: Float
      get(){
        var width = innerPadding
        if (rectType == RectType.ROUNDRECT) {
            width += strokeWidth
        }
        return width
    }

    var rect = RectF()
    var path: Path = Path()
    
    
    fun renderRect(
        canvas: Canvas, rect: RectF,
        isShowBoxBorder: Boolean, isShowBackground: Boolean
    ) {
        when (rectType) {
            RectType.RECT -> {
                if (isShowBackground) canvas.drawRect(rect, backgroundPaint)
                if (isShowBoxBorder) canvas.drawRect(rect, linePaint)
            }
            RectType.ROUNDRECT -> {
                if (isShowBackground) canvas.drawRoundRect(
                    rect, strokeWidth,
                    strokeWidth, backgroundPaint
                )
                if (isShowBoxBorder) canvas.drawRoundRect(
                    rect, strokeWidth,
                    strokeWidth, linePaint
                )
            }
        }
    }


    fun renderCapRect(
        canvas: Canvas, rect: RectF, capHeight: Float,
        showBoxBorder: Boolean, showBackground: Boolean
    ) {
        val centerX = rect.left + rect.width() * 0.5f
        val angleY = rect.bottom
        path.moveTo(rect.left, rect.bottom)
        path.lineTo(rect.left, rect.top)
        path.lineTo(rect.right, rect.top)
        path.lineTo(rect.right, rect.bottom)
        path.lineTo(centerX + capHeight, angleY)
        path.lineTo(centerX, angleY + capHeight)
        path.lineTo(centerX - capHeight, angleY)
        path.close()
        if (showBackground) canvas.drawPath(path, backgroundPaint)
        if (showBoxBorder) canvas.drawPath(path, linePaint)
        path.reset()
    }


    fun renderCapRound(
        canvas: Canvas, rect: RectF, capHeight: Float,
        showBoxBorder: Boolean, showBackground: Boolean
    ) {
        if (!showBackground) return
        val centerX = rect.left + rect.width() * 0.5f
        val AngleY = rect.bottom

        this.rect.left = rect.left + innerPadding
        this.rect.top = rect.top + innerPadding
        this.rect.right = rect.right - innerPadding
        this.rect.bottom = rect.bottom - innerPadding
        backgroundPaint.style = Paint.Style.FILL
        //mPaintBorderLine.setStyle(Style.STROKE);				
        canvas.drawRoundRect(
            this.rect,
            this.strokeWidth,
            this.strokeWidth,
            backgroundPaint
        )
        val pHeight = getPaintFontHeight(backgroundPaint)
        path.moveTo(centerX + capHeight, AngleY - pHeight)
        path.lineTo(centerX, AngleY + capHeight)
        path.lineTo(centerX - capHeight, AngleY - pHeight)
        path.close()
        canvas.drawPath(path, backgroundPaint)
        path.reset()


        /*
		if(showBoxBorder)
		{										
			mPath.moveTo(rect.left, rect.bottom);
			mPath.lineTo(rect.left, rect.top);
			mPath.lineTo(rect.right, rect.top);
			mPath.lineTo(rect.right, rect.bottom);
			mPath.lineTo( centerX + AngleH, AngleY);
			mPath.lineTo( centerX , AngleY + AngleH );
			mPath.lineTo( centerX - AngleH, AngleY);
			mPath.close();		
			canvas.drawPath(mPath, mPaintBorderLine);
			mPath.reset();
		}
		*/
    }


    fun renderRound(
        canvas: Canvas, rect: RectF, capHeight: Float,
        showBoxBorder: Boolean, showBackground: Boolean
    ) {
        this.rect.left = rect.left + innerPadding
        this.rect.top = rect.top + innerPadding
        this.rect.right = rect.right - innerPadding
        this.rect.bottom = rect.bottom - innerPadding
        if (showBackground) canvas.drawRoundRect(
            this.rect,
            this.strokeWidth,
            this.strokeWidth,
            backgroundPaint
        )
        if (showBoxBorder) canvas.drawRoundRect(
            this.rect,
            this.strokeWidth,
            this.strokeWidth,
            linePaint
        )
    }


    /**
     * 绘制边
     * @param canvas    画布
     * @param left    左边距
     * @param top    上边距
     * @param right    右边距
     * @param bottom    底边距
     */
    fun renderBorder(
        type: String, canvas: Canvas,
        left: Float, top: Float, right: Float, bottom: Float
    ) {
        rect.left = left + innerPadding
        rect.top = top + innerPadding
        rect.right = right - innerPadding
        rect.bottom = bottom - innerPadding
        when (rectType) {
            RectType.RECT -> if (type == "CHART") {
                canvas.drawRect(rect, backgroundPaint)
            } else { //BORDER
                canvas.drawRect(rect, linePaint)
            }
            RectType.ROUNDRECT -> if (type == "CHART") {
                canvas.drawRoundRect(
                    rect, strokeWidth,
                    strokeWidth, backgroundPaint
                )
            } else { //BORDER
                canvas.drawRoundRect(
                    rect, strokeWidth,
                    strokeWidth, linePaint
                )
            }
        }
    }
}